<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/treeview/bootstrap-treeview.min.css" type="text/css" />
    <script type="text/javascript" src="/treeview/bootstrap-treeview.min.js"></script>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script type="text/javascript">
    	$(function(){
    	    /*异步请求*/
    	    /*data后台返回的内容,通过回调函数,放到tree菜单里面*/
    	    $.get('/region/getByParentId.do',function (data) {
    	        /*
    	        全局属性,对应整个treeview
    	        初始化,前data是数据源
    	        */
                $("#tree").treeview({
                    //这里是json
                    data:[{text:'全部地区',nodes:data}],//后台返回的数据,传到这里
                    showTags:true,//显示标签
                    //第一个参数,是我们点击的节点,
                    lazyLoad:function (node) {//懒加载,全局节点初始化treeview
                        //这里查询domain提供的子节点,list是子节点, {id:node.id} 当前id等于节点id
                        $.get('/region/getByParentId.do',{id:node.id},function (list) {
                            //第一个参数是要参加的节点,第二个参数是指定添加到哪里(添加到父节点)
                            //点谁,node(父节点)就是谁,点中国地区,父节点就是中国地区,把第一个参数添加到中国地区下面
                            $('#tree').treeview('addNode',[list,node])
                        })
                    },
                    //data参数是当前选中的节点
                    //event
                    onNodeSelected:function (event,data) {
                        console.log(event);
                        //查询子节点的数据    data.id 当前选中节点的id
                        $.get('/region/listChildRegion.do',{id:data.id},function (list) {
                            //存放tbody的变量
                            var temp = ""
                            //循环取出list中的数据
                            $.each(list,function (index, ele) {
                                var btnText = "设为推荐";
                                if (ele.state == 1){
                                    btnText = "取消推荐";
                                }

                                temp += "<tr><td>"+(index+1)+"</td><td>"+ele.name+"</td>"
                                        +'<td><a href="javascript:void(0);" class="editBtn" data-json='+ele.jsonString+'>修改</a></td>'
                                        +'<td><a href="javascript:void(0);" class="changeStateBtn" data-json='+ele.jsonString+'>'+btnText+'</a></td>'
                                        +"</tr>"
                            })
                            $("tbody").html(temp)

                            //编辑按钮绑定点击事件
                            //必须点击之后,产生按钮,才去绑定
                            $(".editBtn").click(function () {

                                //数值为数组,多个表单情况下,取第一个[0]
                                //reset 清空    用css样式加进去的<img>(头像背景)清空不了
                                //$("#editForm")[0].reset()
                                //$("#editForm").clearForm();

                                //reset之后设置回显
                                var json = $(this).data('json')
                                $("#regionId").val(json.id)
                                $("#name").val(json.name)
                                $("#parentId").val(json.parentId)
                                $("#parentName").val(json.parentName)

                                $("#regionModal").modal('show')
                            })

                            //修改状态按钮
                            $(".changeState").click(function () {

                                //reset之后设置回显
                                var json = $(this).data('json')
                                var temp = 0;//普通
                                if(json.state==0){
                                    temp = 1;//推荐
                                }
                                $.post('/region/updateState.do',{id:json.id,state:temp},function (data) {
                                    if(data.success){
                                        $.messager.confirm("温馨提示","操作成功!",function () {
                                            window.location.reload();
                                        })
                                    }else{
                                        $.messager.alert("错误信息",data.msg);
                                    }
                                })

                            })
                        })
                    }
                })
            })

            $("#saveBtn").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    if(data.success){
                        $.messager.confirm("温馨提示","操作成功!",function () {
                            window.location.reload();
                        })
                    }else{
                        $.messager.alert("错误信息",data.msg);
                    }
                })
            })

            /*$(".addBtn").click(function () {
                //获取选中的地区
                var arr = $('#tree').treeview('getSelected')
                if(arr.length > 0){
                    //arr数组状态
                    $("#parentId").val(arr[0].id)  //节点id
                    $("#parentName").val(arr[0].text)    //tree规定的节点名字
                }
                $("#regionModal").modal('show')
            })*/



            $(".addBtn").click(function () {
                var arr = $("#tree").treeview('getSelected')

                if(arr.length > 0){   //大于0就是有选中
                    $("#parentId").val(arr[0].id)
                    $("#parentName").val(arr[0].text)

                }
                $("#regionModal").modal('show')
            })
    	})
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
<#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <div >
                <a  class="btn btn-success addBtn">添加</a>
            </div>
            <div class="row">
                <div class="col-sm-4">
                    <div id="tree"></div>

                </div>
                <div class="col-sm-8" id="info">
                    <table class="table table-striped table-hover" >
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>地区名称</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="regionModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title"> 地区新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm" >
                    <input type="hidden"  id="regionId" name="id">
                    <div class="form-group" >
                        <label for="name" class="col-sm-4 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name"  placeholder="请输入地区的名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parentName">
                            <input type="hidden" class="form-control" id="parentId" name="parent.id">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary " id="saveBtn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


</body>
</html>